<?php
	$validation_rules = array(
		"title" => array(
			"maxlength" => '255'
		)
	);
?>

<?php
$imageData  =   $data['image'];
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'edit';
$allow_tag  =   $data['allow_tag'];
$tag_list   =   $data['tag_list'];

$html=  'update_image.html?act='.$act;

if ($act==='view') {
    FormHelper::setReadonly(true);
    $validation_rules = array();
    $html=  'image_switch.html';
}

$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;
$from    = isset($data['from']) ? $data['from'] : '';

    //pls replace it with real data
    $is_cloned = mt_rand(0,1);
?>

<div id="image" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">

    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo ucwords($act); ?> Image</h2></div>

    <div class="bd">
    
        <?php FormHelper::StartForm('ane', AC::PDES_BASE_URL. $html) ?>
	    <?php FormHelper::ErrorMessages($errors) ?>
        <?php FormHelper::HiddenField('type', 'image') ?>
        <?php FormHelper::HiddenField('id', $imageData['id']) ?>
        <?php FormHelper::HiddenField('from', $from) ?>
		<?php FormHelper::HiddenField('modified_at', $imageData['modified_at']) ?>
        
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Image Basic Information</legend>

    		<div class="form-item" style="float:right;height:260px;">
        		<?php 
                    $thumb  =   $imageData['thumbnail_url'];
                    $source =   $imageData['source_url'];
                ?>
                <a href="<?php echo $source; ?>" target="_blank">
                    <img src="<?php echo $thumb; ?>" alt="" height="200">
                </a>

            </div>

			<?php FormHelper::DropDownList($name='mdes_status', AC::$mdes_status, $imageData['mdes_status'], $label='Status', false, 1); ?>
            <?php FormHelper::ProtectedField($imageData['id'], 'Image ID');?>
			<?php FormHelper::ProtectedField($imageData['created_at'], 'Accession Data/Time') ?>
    		<?php FormHelper::ProtectedField($imageData['programme_id'], 'Programme ID');?>
			<?php FormHelper::ProtectedField($data['programme_title'][0], 'Programme Title (Chinese)') ?>
            <?php FormHelper::ProtectedField($data['programme_title'][1], 'Programme Title (English)') ?>

            <div class="separator"><hr class="ui-widget-content" /></div>

            <div class="form-item">
                <span class="grid-l"><label for="episode_no">On Air Episode Number :</label></span>
                <span class="grid-r">
                <input type="text" value="<?=$imageData['onair_episode_no']?>" class="number" name="onair_episode_no" />
                <input type="hidden" value="<?=$imageData['onair_episode_no_list']?>" name="eps" />
                    <input type="hidden" value="0" name="edit_eps" />
                    <?php if ($act=='edit') { ?>
                    <a href="#" class="btn ui-state-default ui-corner-all btn-edit-eps">Edit Episode</a>
                    <?php } ?>
                </span>
            </div>
                                                                                                                                                        
            <?php 
				FormHelper::ProtectedField($imageData['original_filename'], 'Original Filename');
				FormHelper::TextField('title', $imageData['title'], 'Image Title');
                FormHelper::EditableList($imageData['casts'], array('name'=>'Casts','restrict'=>'0'), "Casts", "cast_dict_id", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

                if (count($errors)>0) {
                    FormHelper::EditableList($imageData['init_tag'][1], $allow_tag[1],'Tags','', 'tag', AC::AJAX_TAG_API_URL, false, 0, true);
                } else {
                    FormHelper::EditableList($tag_list[1], $allow_tag[1],'Tags','', 'tag', AC::AJAX_TAG_API_URL, false, 0, true);
                } 
            ?> 
    	</fieldset>
   	
    	<fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Global Information</legend>
        	    <?php FormHelper::DateTimePicker('online_date_time', $imageData['publish_datetime'], 'Online Date Time (YYYY-MM-DD / HH:MM)') ?>
    		    <?php FormHelper::DateTimePicker('offline_date_time', $imageData['expire_datetime'], 'Offline Date Time (YYYY-MM-DD / HH:MM)') ?>
                
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $imageData['global_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }
                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
?>
                    <label><input type="radio" name="country_allow_deny" value="allow" 
                        <?php echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="country_allow_deny" value="deny" 
                        <?php echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
<?php
    $global_country = isset($imageData['global_country']) ? json_decode($imageData['global_country']) : array();
    FormHelper::CountryList('country','',
        $global_country,
        array(
            'stat'=>AC::$country_stat,
            'service'=>AC::$country_service,
            'mandatory'=>1
        )
    )
?>

                <div class="form-item">
                    <span class="grid-l">
                        <label>Copyright :</label>
                    </span>
                    <span class="grid-r">
                    <?php
                    $copyright = $imageData['copyright'];
                    if('view' === $act)
                    {
                        echo '<p>'.$copyright.'</p>'; 
                    }
                    else{
                        if('TVB' === $copyright || empty($copyright) )
                        {
                            echo '<label><input type="radio" name="copyright" checked="checked" value="TVB" />TVB </label><br />';
                            echo ' <label><input type="radio" name="copyright" />Others: </label>
                                   <input type="text" name="copyright_owner" disabled="disabled"/>';
                        }
                        else
                        {
                            echo '<label><input type="radio" name="copyright" value="TVB" />TVB </label><br />';
                            echo ' <label><input type="radio" checked="checked" name="copyright" />Others: </label>
                                <input type="text" name="copyright_owner" value="'.$copyright.'"/>'; 
                        }
                    }?>
                    </span>
                </div>

        </fieldset>

        <fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Record Status and Access Log</legend>
        	    <?php FormHelper::ProtectedField(AC::$op_status[$imageData['status']], 'Status') ?>
        	    <div class="form-item">&nbsp;</div>
        	    <?php FormHelper::ProtectedField($imageData['created_by'], 'Created By') ?>
        	    <?php FormHelper::ProtectedField($imageData['created_at'], 'Created At') ?>
        	    <?php FormHelper::ProtectedField($imageData['modified_by'], 'Modified By') ?>
        	    <?php FormHelper::ProtectedField($imageData['modified_at'], 'Modified At') ?>
        </fieldset>
    
        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
        <span class="buttons">
            <?php
                switch ($act) {
                    case 'edit':
                        FormHelper::SubmitButton('update', 'Update');
                        FormHelper::ResetButton('reset', 'Reset');
                    	FormHelper::Button('back', 'Back to list');
                    break;
                    case 'view':
						/** modified by vanni for bug no:260 **/
						if($publish ==1){
                            CommonUtils::showUnpublishButton($data, $data['_g']);
                        }else if($data['image']['status'] == AC::STATUS_NEW){
							FormHelper::SubmitButton('edit', 'Edit');
                            CommonUtils::showDeleteButton($data['publishLog']);
						}
						else if($data['image']['status'] ==  AC::STATUS_DELETED){
							FormHelper::SubmitButton('recover', 'Recover');
						}
                    	FormHelper::Button('back', 'Back to list');
                    break;
                }
            ?>
        </span>
        <?php FormHelper::EndForm() ?>

    </div>

</div>

<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
    TVB.Pdes.Vars.isCloned = <?php echo $is_cloned ? 'true' : 'false'; ?>;
</script>
